﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json.Nodes;
using System.Text;
using VM.Common.Result;
using VM.Common.ConfigUntil;
using JsonObject = VM.Common.Result.JsonObject;
using VM.Common.http;
using VM.Entity.EntityMap.Token;
using Newtonsoft.Json;

namespace VM.WebAPI.Controllers
{
    [Route("api/OAuth")]
    [ApiController]
    public class OAuthController : ControllerBase
    {
  
 

        /// <summary>
        /// 获取统一鉴权中心token
        /// </summary>
        /// <returns></returns>
        public static JsonObject GetJwtToken()
        {

          
                string strMessage = "";
                JsonObject reJson = new JsonObject();
                string AuthUrl = ConfigProUntil.GetConfigValuesByKey("AppSettings:AuthServerUrl");
                try
                {
                    //后台地址
                    string strInterFace = AuthUrl + "/api/jwttoken/gettoken";
                    string Param = "{\"publicKey\":\"8EC306FA0FAD4B3BA01E074F38F1FD1E\",\"privateKey\":\"8BA85A8BF3C24369-8A8D6AC1C2CA5359\",\"clientId\":0,\"clientInfo\":\"1\"}";
                    reJson = HttpReuestUtility.GetResponseData(Param, strInterFace);
                }
                catch (Exception e)
                {
                    reJson.code = 401;
                    reJson.msg = "错误：" + e.Message;
                }
                return reJson;
            }


        public static async Task<JsonObject> GetJwtTokenAsync(string AESMemberId,string publicKey,string privateKey)
        {
            string strMessage = "";
            JsonObject reJson = new JsonObject();
            string AuthUrl = ConfigProUntil.GetConfigValuesByKey("AppSettings:AuthServerUrl");
            try
            {
                //后台地址
                string strInterFace = AuthUrl + "/api/jwttoken/gettoken";
                JwtTokenMap jwt = new JwtTokenMap() {
                id = AESMemberId,
                publicKey = publicKey, 
                 privateKey = privateKey,
                 clientInfo = "0",
                 clientId = 9
            };
                
                string Param =JsonConvert.SerializeObject(jwt);
                reJson = await HttpReuestUtility.GetResponseDataAsync(Param, strInterFace); // 使用异步的 HttpReuestUtility.GetResponseDataAsync 方法
            }
            catch (Exception e)
            {
                reJson.code = 401;
                reJson.msg = "错误：鉴权时失败" + e.Message;
            }
            return reJson;
        }


        public static async Task<JsonObject> RefreshJwtTokenAsync(string RefreshToken)
        {
            string strMessage = "";
            JsonObject reJson = new JsonObject();
            string AuthUrl = ConfigProUntil.GetConfigValuesByKey("AppSettings:AuthServerUrl");
            try
            {
                //后台地址
                string strInterFace = AuthUrl + "/api/jwttoken/refreshtoken";
                RefreshJwtTokenMap jwt = new RefreshJwtTokenMap()
                {
                    refreshToken = RefreshToken,
                };

                string Param = JsonConvert.SerializeObject(jwt);
                reJson = await HttpReuestUtility.PostResponseDataAsync(Param, strInterFace); // 使用异步的 HttpReuestUtility.GetResponseDataAsync 方法
            }
            catch (Exception e)
            {
                reJson.code = 401;
                reJson.msg = "错误：" + e.Message;
            }
            return reJson;
        }






    }
}
